import sys
from optparse import OptionParser
from xortable import *

def decodePackageData( data ):
  decodedData = [0]*len(data)
  for i in range( len(data) ):
    
    ord1_idx = i & 0xFF
    ord2_idx = ( i >> 8 ) & 0xFF
    ord3_idx = ( i >> 16 ) & 0xFF

    decodedData[ i ] = chr( ord( data[i] ) ^ Xor_Ord1[ ord1_idx ] ^ Xor_Ord2[ ord2_idx ] ^ Xor_Ord3[ ord3_idx ] )
  return decodedData

def decodePackageFile( filename ):
  f = open( filename, 'rb' )
  data = f.read() 
  f.close()

  return decodePackageData( data )


if __name__ == '__main__':
  parser = OptionParser()
  parser.add_option( '-o', '--output', dest='outfile', default=None )

  (options, args) = parser.parse_args()

  if len( args ) < 1:
    raise Exception( 'No Input File Provided' )

  if options.outfile == None:
    options.outfile = '{0}.out.bin'.format( args[0] )

  f = open( options.outfile, 'wb' )
  f.write( ''.join( decodePackageFile( args[0] ) ) )
  f.close()








